angular.module("application")
.controller('homeCtrl', ['$scope', '$http', '$location', '$timeout', 'effectProcess', 'BASE_URL', 
	function($scope, $http, $location, $timeout, effectProcess, BASE_URL) 
{	
	// Prevent Effect to other Page
	$scope.page = 'home';
	effectProcess.init($scope.page);

	effectProcess.renderAllImages().then(function() {
		$timeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},500)
		$timeout(function() {
			wowInit();
		},480);
	})	

	$scope.lsResult = [];

	$scope.submitSearch = function() {
		$http.post(BASE_URL + 'index.php/application_api/lesson/findLesson')
		.then(function(promise) {
			$scope.lsResult = promise.data;
		});
	}

	$http.post(BASE_URL + 'application_api/classes/getClasses')
	.then(function(promise) {
		$scope.classes = promise.data;
	})
}])

// ====================================================================================
// ====================================================================================

.factory('effectProcess', function($q) {
	var animateGuider = function() {
		setInterval(function() {
			$('.scroll-guider').removeClass('bounce animated');
			setTimeout(function() {
				$('.scroll-guider').addClass('animated bounce');
			},1000)
		}, 4000);
	}
	var sizeParallax = function() {
		dis = viewport();			
		$('.parallax').css({
			"width": dis.width + 'px',
			"height": dis.height - 35 + 'px'
		});
	}

	return {
		init: function(page) {

			$('body').addClass('auto-scroll');
			fullPageEffect();
			$('.parallax').parallax();
			sizeParallax();
			$(window).resize(function() {
				sizeParallax();
			});

			$(".scroll-guider i.fa-chevron-down").click(function() {
				nextP = $(this).parent().parent().next();
				console.log(nextP);
				$("html body").animate({
					scrollTop: $(nextP).offset().top - 35 
				}, 1000, 'swing');
			});
			$(".scroll-guider i.fa-chevron-up").click(function() {
				prevP = $(this).parent().parent().prev();
				$("html body").animate({
					scrollTop: $(prevP).offset().top - 35 
				}, 1000, 'swing');
			});


			// Auto Scroll
			var scr = false;
			var turn = null;

			$(window).scroll(function() {
				scr = true;
				if(turn) {
					clearTimeout(turn);
				}
				turn = setTimeout(function() {
					scr = false;

					pageH = viewport().height - 35;

					scrollVal = $('body.auto-scroll').scrollTop();
					mod = scrollVal % pageH;
					
					if(mod == 0 || scrollVal < 35 || scr) return;

					if(mod <= 100 && !scr) {
						$('body.auto-scroll').animate({
							scrollTop: scrollVal - mod
						}, 300, 'swing');
					}
					else if(!scr) {
						for (i=1; i < 100; i++) {
							pg = (scrollVal + i);
							if((pg % pageH) == 0) {
								$('body.auto-scroll').animate({
									scrollTop: pg
								}, 300, 'swing');
								break;
							}
						}
					}
				}, 500);
			});
			// setInterval(function() {
			// 	// scr = false;
					
			// }, 1000);

			animateGuider();
		},
		renderAllImages: function() {
			deferred = $q.defer();
			var total_img = $('body img').length;
			var imgLoaded = 0;
			$('body img').each(function() {
				fakeSrc = $(this).attr('src');
				$("<img/>").attr('src', fakeSrc).css('display', 'none').load(function() {
					imgLoaded++;
					if(imgLoaded >= total_img) {
						deferred.resolve();
					}
				});
			});
			setTimeout(function() {
				deferred.resolve();
			}, 10000);
			return deferred.promise;			
		}
	};
})

// ==========================================================================================================
// ==========================================================================================================
// ==========================================================================================================
// ==========================================================================================================


.controller('searchCtrl', ['$scope', '$http', '$location', '$routeParams', 'BASE_URL', 'searchCache', 
	function($scope, $http, $location, $routeParams, BASE_URL, searchCache) 
{	
	$scope.searchF = $routeParams.str;
	$scope.viewSection= 1;
	$scope.lessonList = null;

	$scope.$on('$viewContentLoaded', function() { // Load content effects after content is loaded
		getSearch($scope.searchF).then(function() {
			setTimeout(function() {
				$scope.$apply(function() {
					$scope.$parent.loadingView = false;
				});
			},100);
		})		
	});

	$scope.getSearch = function(str) {
		$location.path('/search/' + $scope.searchF);
	}

	getSearch = function(str) {
		return $http.post(BASE_URL + 'application_api/lesson/searchLessons/' + str)
		.then(function(promise) {
			$scope.lessonList = promise.data;

			setTimeout(function() {
				resizeList();
			},120)
		});
	}

	resizeList = function() {
		$('.lesson-list li').each(function() {
			w = $(this).innerWidth();
			h = w * 4 / 6;
			$(this).css('height', h);
		});

		$(window).resize(function() {
			$('.lesson-list li').each(function() {
				w = $(this).innerWidth();
				h = w * 4 / 6;
				$(this).css('height', h);
			});
		})
	}
}]);

// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================

wowInit = function() {
	new WOW().init();
	$('.banner .drumm img').addClass('spin');
	// setTimeout(function() {
	// 	$('.banner .drumm img').removeClass('wow animated');
	// 	$('.banner .drumm img').removeAttr('style');
	// }, 3500);
}